<?php

namespace App\Controller\LazyCommon;

use App\Base\BaseConfig;
use App\Base\BaseController4Admin;
use App\Model\LazyCommon\AdminMenuModel;
use App\Model\LazyCommon\UserModel;
use App\Model\LazyCommon\UserGroupsModel;
use Symfony\Component\HttpFoundation\Request;
use App\Base\BaseController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

/**
 * @Route("/admin")
 */
class AdminIndexController extends BaseController4Admin
{




    /**
     * 后台数据统计主页
     * @Route("/main/welcome",name="adminWelcomePage")
     */
    public function welcomeAction()
    {
        $title = "管理首页";
        $data = array(
            "title"   => $title,
        );

        return $this->render("LazyCommon/welcome.html.twig",$data);
    }

    /**
     * 后台数据统计主页
     * @Route("/main/center",name="adminCenterPage")
     */
    public function centerAction()
    {
        $title = "管理首页";
        $data = array(
            "title"   => $title,
        );

        //获取所有用户
        $UserModel = new UserModel($this->getDoctrine());
        $qbUser = $UserModel->getQueryBuilder();
        $qbUser->select("count(A.id) as num,groups,B.name")
            ->from("User","A")
            ->where("A.groups < 100")
            ->andWhere("A.groups > 2")
            ->andWhere("A.groups < 10")
            ->leftJoin("A","User_Groups","B","A.groups = B.id")
            ->orderBy("num","DESC")
            ->groupBy("A.groups");
        $data["user_num"] = $qbUser->execute()->fetchAll();
        //今日新增
        $UserModel = new UserModel($this->getDoctrine());
        $qbUser = $UserModel->getQueryBuilder();
        $qbUser->select("count(A.id) as num,groups,B.name")
            ->from("User","A")
            ->where("A.groups < 100")
            ->andWhere("A.addTime >".$this->getDayStart())
            ->andWhere("A.groups > 2")
            ->andWhere("A.groups < 10")
            ->leftJoin("A","User_Groups","B","A.groups = B.id")
            ->orderBy("num","DESC")
            ->groupBy("A.groups");
        $data["user_num_day"] = $qbUser->execute()->fetchAll();
        //周新增
        $UserModel = new UserModel($this->getDoctrine());
        $qbUser = $UserModel->getQueryBuilder();
        $qbUser->select("count(A.id) as num,groups,B.name")
            ->from("User","A")
            ->where("A.groups < 100")
            ->andWhere("A.groups > 2")
            ->andWhere("A.groups < 10")
            ->andWhere("A.addTime >".$this->getWeekStart())
            ->leftJoin("A","User_Groups","B","A.groups = B.id")
            ->orderBy("num","DESC")
            ->groupBy("A.groups");
        $data["user_num_week"] = $qbUser->execute()->fetchAll();
        //月新增
        $UserModel = new UserModel($this->getDoctrine());
        $qbUser = $UserModel->getQueryBuilder();
        $qbUser->select("count(A.id) as num,groups,B.name")
            ->from("User","A")
            ->where("A.groups < 100")
            ->andWhere("A.groups > 2")
            ->andWhere("A.groups < 10")
            ->andWhere("A.addTime >".$this->getMonthStart())
            ->leftJoin("A","User_Groups","B","A.groups = B.id")
            ->orderBy("num","DESC")
            ->groupBy("A.groups");
        $data["user_num_month"] = $qbUser->execute()->fetchAll();


        // 0 未支付 100 代发货 200 待收货 900 已完成
        $data["order_status_name"] = [
            "0" => "未支付",
            "100" => "待发货",
            "200" => "待收货",
            "900" => "已完成",
        ];
        //获取所有订单
        $qbOrder = $UserModel->getQueryBuilder();
        $qbOrder->select("count(A.id) as num ,sum(total) as money ,status")
            ->from("Orders","A")
            ->where("A.status > -1")
            ->orderBy("status","ASC")
            ->groupBy("A.status");
        $data["order_num"] = $qbOrder->execute()->fetchAll();
        //获今日订单
        $qbOrder = $UserModel->getQueryBuilder();
        $qbOrder->select("count(A.id) as num ,sum(total) as money ,status")
            ->from("Orders","A")
            ->where("A.status > -1")
            ->andWhere("A.addTime > '".date(YYMMDDHHIISS,$this->getDayStart())."'")
            ->orderBy("status","ASC")
            ->groupBy("A.status");
        $data["order_num_day"] = $qbOrder->execute()->fetchAll();
        //获本周订单
        $qbOrder = $UserModel->getQueryBuilder();
        $qbOrder->select("count(A.id) as num ,sum(total) as money ,status")
            ->from("Orders","A")
            ->where("A.status > -1")
            ->andWhere("A.addTime > '".date(YYMMDDHHIISS,$this->getWeekStart())."'")
            ->orderBy("status","ASC")
            ->groupBy("A.status");
        $data["order_num_week"] = $qbOrder->execute()->fetchAll();
        //获本月订单
        $qbOrder = $UserModel->getQueryBuilder();
        $qbOrder->select("count(A.id) as num ,sum(total) as money ,status")
            ->from("Orders","A")
            ->where("A.status > -1")
            ->andWhere("A.addTime > '".date(YYMMDDHHIISS,$this->getMonthStart())."'")
            ->orderBy("status","ASC")
            ->groupBy("A.status");
        $data["order_num_month"] = $qbOrder->execute()->fetchAll();

        //升级数据-总
        $qbLevelUp = $UserModel->getQueryBuilder();
        $qbLevelUp->select("count(A.id) as num")
            ->from("user_level_up","A")
            ->where("A.check_status = 1")
        ;
        $rst = $qbLevelUp->execute()->fetchAll();
        $data["levelup_num"] = $rst?$rst[0]["num"]:0;
        //升级数据-今日
        $qbLevelUp = $UserModel->getQueryBuilder();
        $qbLevelUp->select("count(A.id) as num")
            ->from("user_level_up","A")
            ->where("A.check_status = 1")
            ->andWhere("A.check_time >".$this->getDayStart())
        ;
        $rst = $qbLevelUp->execute()->fetchAll();
        $data["levelup_num_day"] = $rst?$rst[0]["num"]:0;
        //升级数据-本周
        $qbLevelUp = $UserModel->getQueryBuilder();
        $qbLevelUp->select("count(A.id) as num")
            ->from("user_level_up","A")
            ->where("A.check_status = 1")
            ->andWhere("A.check_time >".$this->getWeekStart())
        ;
        $rst = $qbLevelUp->execute()->fetchAll();
        $data["levelup_num_week"] = $rst?$rst[0]["num"]:0;
        //升级数据-本月
        $qbLevelUp = $UserModel->getQueryBuilder();
        $qbLevelUp->select("count(A.id) as num")
            ->from("user_level_up","A")
            ->where("A.check_status = 1")
            ->andWhere("A.check_time >".$this->getMonthStart())
        ;
        $rst = $qbLevelUp->execute()->fetchAll();
        $data["levelup_num_month"] = $rst?$rst[0]["num"]:0;

        return $this->render("LazyCommon/center.html.twig",$data);
    }
}
